package com.fypWebService;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class AccountEnquiry
 */
public class AccountEnquiry extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AccountEnquiry() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Boolean validCookieBool = false;
		Cookie[] cookies = request.getCookies();
		String userHash = getClientHash(cookies);
		SSOClientService ssocs = new SSOClientService();
		RBKClientService rbkcs = new RBKClientService();

		
	
		if(userHash!=null){
			if(ssocs.checkTrust1FA(userHash)||ssocs.checkTrust2FA(userHash)){
				validCookieBool=true;
				System.out.println("<fypWebService><userHash>userHash = "+userHash);
				
			}	
		}
		else{
			System.out.println("<fypWebService><AccountEnquiry>userHash from Client is null!");
		}
		
		
		if(validCookieBool){
			String userName=ssocs.getLoginName(userHash);
			System.out.println("<fypWebService><AccountEnquiry>userName= "+userName);
			
			
			//testing sector
		/*	
			List<AccountLinkDataStructure> x = rbkcs.getPayeeList(userName);
			System.out.println("<fypWebService><AccountEnquiry> "+x.get(0).getPayeeAccount());
			System.out.println("<fypWebService><AccountEnquiry> "+x.get(0).getAccountLinkHash());
		*/
		/*
			payPayee2FAData b = rbkcs.payPayee2FA1("ashisauhfisuhfisuahdfiuashHIHIUHUADasnfd", 0.5);
			System.out.println("<fypWebService><AccountEnquiry><testing payPayee2FA1>starts");
			System.out.println("<fypWebService><AccountEnquiry><testing payPayee2FA1>preHash= "+b.getPreHash());
			System.out.println("<fypWebService><AccountEnquiry><testing payPayee2FA1>account= "+b.getPayeeAccount());
			System.out.println("<fypWebService><AccountEnquiry><testing payPayee2FA1>amount = "+b.getAmount());
			System.out.println("<fypWebService><AccountEnquiry><testing payPayee2FA1>ends");
			//end of testing sector
		*/
			
		/*	
			payPayee2FAData c = rbkcs.payPayee2FA2("ashisauhfisuhfisuahdfiuashHIHIUHUADasnfd","123456");
			System.out.println("<fypWebService><AccountEnquiry><testing payPayee2FA2>starts");
			System.out.println("<fypWebService><AccountEnquiry><testing payPayee2FA2>preHash      = "+c.getPreHash());
			System.out.println("<fypWebService><AccountEnquiry><testing payPayee2FA2>account      = "+c.getPayeeAccount());
			System.out.println("<fypWebService><AccountEnquiry><testing payPayee2FA2>amount       = "+c.getAmount());
			System.out.println("<fypWebService><AccountEnquiry><testing payPayee2FA2>2fa2succeess = "+c.getPayPayee2FA2Success());
			System.out.println("<fypWebService><AccountEnquiry><testing payPayee2FA2>ends");
			//end of testing sector
		*/	
		/*
			String payeeNumberString = "6666666666";
			long payeeNumber = Long.parseLong(payeeNumberString);
			AddPayee2FAData d = rbkcs.addPayee2FA1(userName,1111111111, payeeNumber);
			System.out.println("<fypWebService><AccountEnquiry><testing addPayee2FA1>starts");
			System.out.println("<fypWebService><AccountEnquiry><testing addPayee2FA1>AddPayee2FA1Success = "+d.getAddPayee2FA1Success());
			System.out.println("<fypWebService><AccountEnquiry><testing addPayee2FA1>AddPayee2FA2Success = "+d.getAddPayee2FA2Success());
			System.out.println("<fypWebService><AccountEnquiry><testing addPayee2FA1>preHash             = "+d.getPreHash());
			System.out.println("<fypWebService><AccountEnquiry><testing addPayee2FA1>indexHash           = "+d.getIndexHash());
			System.out.println("<fypWebService><AccountEnquiry><testing addPayee2FA1>ends");
		*/	
		/*	
			AddPayee2FAData e = rbkcs.addPayee2FA2("ashisauhfisuhfisuahdfiuashHIHIUHUADasnfd","123456");
			System.out.println("<fypWebService><AccountEnquiry><testing addPayee2FA2>starts");
			System.out.println("<fypWebService><AccountEnquiry><testing addPayee2FA2>AddPayee2FA1Success = "+e.getAddPayee2FA1Success());
			System.out.println("<fypWebService><AccountEnquiry><testing addPayee2FA2>AddPayee2FA2Success = "+e.getAddPayee2FA2Success());
			System.out.println("<fypWebService><AccountEnquiry><testing addPayee2FA2>preHash             = "+e.getPreHash());
			System.out.println("<fypWebService><AccountEnquiry><testing addPayee2FA2>indexHash           = "+e.getIndexHash());
			System.out.println("<fypWebService><AccountEnquiry><testing addPayee2FA2>ends");
		*/	
			
			//end of testing sector
			List<AccountClass> accountList = rbkcs.getAccount(userName);
			String[][] records = getStringArray(accountList);
			request.setAttribute("records", records);
			
			
			RequestDispatcher dispatcher = request.getRequestDispatcher("/AccountEnquiry.jsp");
			if (dispatcher != null) dispatcher.forward(request, response);
		}
		else{
			RequestDispatcher dispatcher = request.getRequestDispatcher("/timedOut.jsp");
			if (dispatcher != null) dispatcher.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		
		
		
		
	}
	
	private String getClientHash(Cookie[] cookies){
		 for(int i=0; i<cookies.length; i++) {
		      Cookie cookie = cookies[i];
		      System.out.println("cookieName = "+ cookie.getName());
		     
		      if (cookie.getName().equals("fypUserHash")){
		    	  System.out.println("returning = "+cookie.getValue());
		    	  return(cookie.getValue());
		      }
		       
		    }
		return null;
		
	}
	
	private String[][] getStringArray(List<AccountClass> accountList){
		int row = accountList.size() + 1;	
		String [][] object = new String[row][4];
		object[0][0] = "Account Name";
		object[0][1] = "Account Number";
		object[0][2] = "Current Balance";
		object[0][3] = "Available Balance";
		
		int i = 0;
		while(i!=(row-1)){
			object[i][0] = accountList.get(i).getAccountType();
			object[i][1] = Long.toString(accountList.get(i).getAccountNumber());
			object[i][2] = Double.toString(  roundTwoDecimals( accountList.get(i).getCurrentBalance()));
			object[i][3] = Double.toString( roundTwoDecimals( accountList.get(i).getAvailableBalance()));
			i++;
		}
		return object;
	}
	
	private double roundTwoDecimals(double d) {
    	DecimalFormat twoDForm = new DecimalFormat("#.##");
    	return Double.valueOf(twoDForm.format(d));
	}
	

}
